<?php
//---------------------------------------------------------------------------------------------------------------------------
error_reporting(E_ALL);
    require_once('../../../config.inc.php');

    include_once($config['app_dir'] . '/includes/column.class.php');
    include_once($config['app_dir'] . '/includes/pager.class.php');
    include_once($config['app_dir'] . '/includes/FormParams.class.php');
    include_once($config['app_dir'] . '/includes/params.class.php');
    include_once($config['app_dir'] . '/includes/db.class.php');
    require_once($config['app_dir'] . '/classes/staff.class.php');

    $Staff = new objStaff($config['app_id']);
    if( 3!=$Staff->UserGroupID() ) die('You have not permission to see this page. ');//var_dump($Staff->UserGroupID());

    include_once($config['app_dir'] . '/classes/admins/sql/translation.sql.php');
    include_once($config['app_dir'] . '/classes/admins/const/translation.const.php');
    //------------------------------------------------------------------------------------------------------------------------
    $ColumnSet = $LangColumnSet;
    //------------------------------------------------------------------------------------------------------------------------
    $DataBase = objDataBase::GetInstance();
    $Session = new objSession($config['app_id']);

    //------------------------------------------------------------------------------------------------------------------------
    // Form Select Language
    //------------------------------------------------------------------------------------------------------------------------
    $ParamsLangSelect = new objFormParams('post', 'selectlang_');
    if( $params_lang = $ParamsLangSelect->getAssocArray() ) $Session->Put('lang_const__lang', $params_lang);
       elseif( $Session->isExists('lang_const__lang') ) $params_lang = $Session->Get('lang_const__lang');

    $ParamsLang = new objParamList('lang');
    $ParamsLang->Build($DataBase->handler, "SELECT id as value, name FROM fa_language");
    $lang['list'] = $ParamsLang->getAssocArray();

    if( !$params_lang ) $params_lang['lang'] = $lang['list'][0]['value'];

    $lang_id = $params_lang['lang'];

    $lang['value'] = $lang_id;
    $lang['name'] = "selectlang_lang";
    $lang['caption'] = "Language: ";

    $SelectForm = new objTemplate();
    $SelectForm->Assign('select', $lang);
    //------------------------------------------------------------------------------------------------------------------------
    // Form Select Resource
    //------------------------------------------------------------------------------------------------------------------------
    $ParamsResourceSelect = new objFormParams('post', 'selectresource_');
    if( $params_resource = $ParamsResourceSelect->getAssocArray() ) $Session->Put('resource_const__resource', $params_resource);
       elseif( $Session->isExists('resource_const__resource') ) $params_resource = $Session->Get('resource_const__resource');

    $ParamsResource = new objParamList('resource');
    $ParamsResource->Build($DataBase->handler, "SELECT distinct resource as name,resource as value FROM fa_const ORDER BY name asc");
    $resource['list'] = $ParamsResource->getAssocArray();

    if (!$params_resource) $params_resource['resource'] = $resource['list'][0]['value'];


    $resource_id=$params_resource['resource'];

    $resource['value'] = $resource_id;
    $resource['name'] = "selectresource_resource";
    $resource['caption'] = "Resource: ";

    $ResourceForm = new objTemplate();
    $ResourceForm->Assign('select', $resource);

    //-------------------------------------------------------------------------------------------------------
    $query_main  = $translation_query_main . " where lang.id=  $lang_id and const.resource='$resource_id'";
    $query_count = $translation_query_count . " where lang.id=  $lang_id  and const.resource='$resource_id'";
    //-------------------------------------------------------------------------------------------------------
    // Make changes
    //-------------------------------------------------------------------------------------------------------
    $ParamsConst = new objFormCompositeParams('post', 'data_','__', true);

    if( ( !$ParamsConst->isEmpty() || 'edit'==$ParamsConst->getOperation() ) && $lang_id) {
       $params_const = $ParamsConst->Slice('const_id');
       $DataBase->handler->StartTrans();//start transaction

        foreach($params_const as $item)
            foreach($item as $name => $value) {
                $fields = array();
                $fields['language_id'] = $lang_id;
                $fields['const_id'] = $name;
                $fields['text'] = trim($value);

                if ( ''==$fields['text'] || ' '==$fields['text']  ) $fields['text'] = 'null';

                $DataBase->handler->Replace('fa_label', $fields,  array('language_id', 'const_id'), true);
            }
        $DataBase->handler->Execute("DELETE FROM fa_label WHERE text is null");
        $result = $DataBase->handler->CompleteTrans();//complete transaction
    }
    //-------------------------------------------------------------------------------------------------------
    // Pager
    //-------------------------------------------------------------------------------------------------------
    $Pager = new objRsGridPager($ColumnSet, $query_main, $query_count, 'lang_const');
    $Pager->rows_steps = array(10,20,30);
    $Pager->SetTitle($ColumnSet->title . $ParamsLang->Item($lang_id)->name . ' (' . $ParamsResource->Item($resource_id)->name . ')');
    //-------------------------------------------------------------------------------------------------------
    // Composite Page
    //-------------------------------------------------------------------------------------------------------
    $Page = new objTemplate();
    $Page->Assign('content', $SelectForm->Fetch('snippet/form_singleselect.tpl') .
                             $ResourceForm->Fetch('snippet/form_singleselect.tpl') .
                             $Pager->Render('admins/lang/grid_lang.tpl'));
	$Page->Display('admins/lang/page_lang.tpl');
//---------------------------------------------------------------------------------------------------------------------------
?>
